#!/bin/bash

#=======================================================================
# git-lc
# File ID: 81eb015e-87fb-11e0-9674-fefdb24f8e10
# List all Git branches where all the specified refs exists
# License: GNU General Public License version 2 or later.
#=======================================================================

urevs=$*
test -z "$urevs" && urevs=HEAD
count=0
unset revs
for u in $urevs; do
    git log -1 $u &>/dev/null || { echo $u: Invalid ref >&2; exit 1; }
    revs="$revs $(git branch -a --contains=$u | cut -c3- | grep -v -e '(no branch)' -e '->' -e "^$u\$")"
    count=$[$count + 1]
done
echo $revs | fmt -1 | tr -d ' ' | sort | uniq -c | tr -s ' ' \
    | grep "^ $count " | cut -f 3 -d ' ' | sort
